package com.ll.gulimall.serach.thread;

import java.util.concurrent.*;

public class ThreadTest {

    static ExecutorService executorService = Executors.newFixedThreadPool(10);//初始化线程池 大小10

    public static void main(String[] args) throws ExecutionException, InterruptedException {
      /*  CompletableFuture.runAsync(() -> {
            System.out.println(10 / 2 + "：结果");
        }, executorService);*/

     /*   CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            return 10 / 0;
        }, executorService).whenCompleteAsync((res, excption) -> {
            //监听到异常
            System.out.println("结果是:" + res);
            System.out.println("异常是:" + excption);
        }).exceptionally((throwable) -> {
            //捕获异常可以处理返回默认值
            return 10;
        });*/
  /*      System.out.println("start");
        CompletableFuture<Object> completable1 = CompletableFuture.supplyAsync(() -> {
            return 10 / 2;
        }, executorService);
        CompletableFuture<Object> completable2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2执行完毕");

            return "hello";
        }, executorService);

        CompletableFuture<String> apply = completable1.applyToEitherAsync(completable2, res -> {
            return "上一次结果:" + res;
        }, executorService);
        System.out.println(apply.get());*/

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品信息");
            return "查询商品图片";
        }, executorService);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品信息");
            return "查询商品属性";
        }, executorService);
        CompletableFuture<String> futureInfo = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品信息");
            return "查询商品信息";
        }, executorService);
        //多任务组合 全部完成
      /*  CompletableFuture<Void> allOf = CompletableFuture.allOf(futureAttr, futureImg, futureInfo);
        allOf.get();//获取所有结果
        System.out.println("结果:" + futureAttr.get() + " ; " + futureImg.get() + " ; " + futureInfo.get());*/
        //多任务组合 一个完成即可
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureAttr, futureImg, futureInfo);
        System.out.println("结果:" + anyOf.get().toString());
    }

    /**
     * 线程初始化的四种方式
     * 1、继承Thread      new Thread01().start();//启动线程
     * 2、实现Runnable     new Thread(new Runnable01()).start();
     * 3、实现Callable + FutureTask
     * FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable01());
     * integerFutureTask.run();//执行线程
     * Integer integer = integerFutureTask.get();// 获取线程执行结果 堵塞状态
     * 4、线程池
     *
     * @param args
     */
    static Integer test = 1;


    public static void thread(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main--------start" + Thread.currentThread().getId());

        Future<Integer> submit = executorService.submit(new Callable01());//提交任务
        System.out.println(submit.get());//获取结果 线程堵塞状态
        System.out.println("main-------------end");
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(7, 20,
                10, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100000), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    public static class Callable01 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {

            return 10 / 2;
        }
    }

    public static class Runnable01 implements Runnable {

        @Override
        public void run() {
            System.out.println("执行Runnable");
        }
    }

    public static class Thread01 extends Thread {

        int a = 1;


    }
}
